package com.example.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.pojo.PageData;
import com.example.pojo.ResultBean;
import com.example.pojo.WasteType;
import com.example.service.IWasteTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lyj
 * @since 2023-02-16
 */
@RestController
@RequestMapping("/waste-type")
public class WasteTypeController {
    @Autowired
    private IWasteTypeService wasteTypeService;

    /**
     * 分页条件查询
     * @param pageData
     * @param map
     * @return
     */
    @GetMapping("/list")
    public ResultBean list(PageData pageData, @RequestParam Map<String,Object> map){
        Page<WasteType> page = new Page<>(pageData.getPageNum(),pageData.getPageSize());
        wasteTypeService.lambdaQuery()
                .like(!ObjectUtils.isEmpty(map.get("name")),WasteType::getName,map.get("name"))
                .like(!ObjectUtils.isEmpty(map.get("code")),WasteType::getCode,map.get("code"))
                .page(page);
        return ResultBean.success(page);
    }

    /**
     * 查询全部
     * @return
     */
    @GetMapping("queryAll")
    public ResultBean queryAll(){
        return ResultBean.success(wasteTypeService.list());
    }

    /**
     * 新增或修改
     * @param wasteType
     * @return
     */
    @PostMapping("/saveOrUpdate")
    public ResultBean saveOrUpdate(@RequestBody WasteType wasteType){
        System.out.println("新增或修改的数据：" + wasteType);
        boolean rs = wasteTypeService.saveOrUpdate(wasteType);
        return rs ? ResultBean.success() : ResultBean.error();
    }

    /**
     * 根据ID删除
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public ResultBean delete(@PathVariable("id") Long id){
        System.out.println("要删除的Id" + id);
        boolean rs = wasteTypeService.removeById(id);
        return rs ? ResultBean.success() : ResultBean.error();
    }
}
